package com.chagee.store.operation.server.component;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.List;
import java.util.Properties;

/**
 * excel设置表头
 * @Author : hk
 * @Date : 2024/12/5
 */
public class ReportExportCellWriteHandler extends AbstractVerticalCellStyleStrategy {

	private String title;

	PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");

	public ReportExportCellWriteHandler(String title) {
		this.title = title;
	}

	@Override
	public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
								 Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
		if (head != null) {
			List<String> headNameList = head.getHeadNameList();
			if (CollectionUtils.isNotEmpty(headNameList)) {
				Properties properties = new Properties();
				properties.setProperty("title", title);
				headNameList.replaceAll(value -> propertyPlaceholderHelper.replacePlaceholders(value, properties));
			}
		}
	}
}

